Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 520)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.92195 12.92387 12.92580 12.92774 12.92968 12.93161 12.93352 12.93540
## [9] 12.93723 12.93901 12.94074 12.94239 12.94396 12.94544 12.94681 12.94808
## [17] 12.94923 12.95024 12.95112 12.95184 12.95240 12.95279 12.95304 12.95318
## [25] 12.95323 12.95318 12.95304 12.95281 12.95250 12.95212 12.95166 12.95114
## [33] 12.95056 12.94992 12.94923 12.94850 12.94772 12.94690 12.94605 12.94518
## [41] 12.94428 12.94336 12.94243 12.94149 12.94055 12.93961 12.93867 12.93775
## [49] 12.93684 12.93595 12.93499 12.93389 12.93265 12.93128 12.92978 12.92819
## [57] 12.92649 12.92470 12.92284 12.92091 12.91892 12.91688 12.91481 12.91271
## [65] 12.91060 12.90848 12.90636 12.90426 12.90218 12.90014 12.89814 12.89620
## [73] 12.89432 12.89252 12.89081 12.88919 12.88768 12.88628 12.88453 12.88198
## [81] 12.87869 12.87472 12.87012 12.86497 12.85931 12.85321 12.84673 12.83993
## [89] 12.83286 12.82559 12.81817 12.81067 12.80314 12.79564 12.78824 12.78099
## [97] 12.77395 12.76718 12.76075 12.75471 12.74911 12.74403 12.73951 12.73562
## [105] 12.73242 12.72997 12.72769 12.72499 12.72193 12.71857 12.71496 12.71115
## [113] 12.70720 12.70316 12.69909 12.69505 12.69108 12.68725 12.68361 12.68020
## [121] 12.67710 12.67434 12.67199 12.67011 12.66873 12.66793 12.66776 12.66826
## [129] 12.66950 12.67153 12.67440 12.67817 12.68290 12.68938 12.69817 12.70894
## [137] 12.72135 12.73508 12.74981 12.76519 12.78090 12.79662 12.81201 12.82674
## [145] 12.84048 12.85291 12.86369 12.87458 12.88732 12.90162 12.91714 12.93360
## [153] 12.95066 12.96803 12.98539 13.00242 13.01882 13.03428 13.04848 13.06112
## [161] 13.07187 13.08383 13.09981 13.11914 13.14112 13.16506 13.19028 13.21609
## [169] 13.24180 13.26671 13.29015 13.31142 13.32984 13.34471 13.35534 13.36416
## [177] 13.37391 13.38445 13.39562 13.40727 13.41925 13.43141 13.44360 13.45565
## [185] 13.46743 13.47877 13.48952 13.49954 13.50866 13.51674 13.52363 13.52916
## [193] 13.53320 13.53558 13.53615 13.53476 13.53127 13.52551 13.51739 13.50709
## [201] 13.49483 13.48087 13.46543 13.44874 13.43106 13.41261 13.39364 13.37437
## [209] 13.35504 13.33590 13.31717 13.29910 13.27927 13.25548 13.22829 13.19828
## [217] 13.16601 13.13204 13.09694 13.06128 13.02563 12.99055 12.95660 12.92436
## [225] 12.89439 12.86725 12.83878 12.80501 12.76683 12.72513 12.68081 12.63475
## [233] 12.58785 12.54100 12.49508 12.45100 12.40963 12.37187 12.33862 12.31076
## [241] 12.28501 12.25775 12.22934 12.20015 12.17055 12.14090 12.11158 12.08294
## [249] 12.05536 12.02920 12.00483 11.98262 11.96294 11.94614 11.93238 11.92131
## [257] 11.91260 11.90592 11.90092 11.89728 11.89465 11.89271 11.89111 11.88952
## [265] 11.88761 11.88503 11.88145 11.87654 11.87195 11.86946 11.86888 11.87005
## [273] 11.87278 11.87692 11.88227 11.88867 11.89595 11.90393 11.91243 11.92129
## [281] 11.93032 11.93936 11.94823 11.95676 11.96477 11.97209 11.97854 11.98395
## [289] 11.98815 11.99097 11.99222 11.99174 11.98935 11.98487 11.97936 11.97392
## [297] 11.96850 11.96303 11.95745 11.95169 11.94571 11.93943 11.93280 11.92575
## [305] 11.91822 11.91016 11.90149 11.89215 11.88033 11.86462 11.84562 11.82395
## [313] 11.80019 11.77496 11.74884 11.72245 11.69639 11.67124 11.64763 11.62614
## [321] 11.60737 11.59194 11.57608 11.55611 11.53274 11.50665 11.47855 11.44914
## [329] 11.41912 11.38919 11.36004 11.33239 11.30693 11.28435 11.26536 11.25066
## [337] 11.24096 11.23360 11.22570 11.21752 11.20934 11.20146 11.19414 11.18768
## [345] 11.18234 11.17842 11.17618 11.17592 11.17791 11.18244 11.18988 11.20020
## [353] 11.21309 11.22820 11.24522 11.26381 11.28365 11.30440 11.32575 11.34736
## [361] 11.36891 11.39006 11.41050 11.42988 11.45033 11.47393 11.50031 11.52907
## [369] 11.55986 11.59229 11.62599 11.66057 11.69567 11.73090 11.76589 11.80027
## [377] 11.83365 11.86566 11.89592 11.92406 11.95346 11.98729 12.02478 12.06521
## [385] 12.10782 12.15187 12.19661 12.24131 12.28522 12.32759 12.36769 12.40476
## [393] 12.43807 12.46686 12.49511 12.52679 12.56123 12.59775 12.63565 12.67425
## [401] 12.71286 12.75081 12.78739 12.82193 12.85375 12.88214 12.90644 12.92595
## [409] 12.94236 12.95780 12.97226 12.98573 12.99819 13.00963 13.02004 13.02941
## [417] 13.03772 13.04496 13.05112 13.05619 13.06015 13.06298 13.06354 13.06093
## [425] 13.05554 13.04777 13.03801 13.02666 13.01410 13.00072 12.98693 12.97311
## [433] 12.95966 12.94697 12.93543 12.92543 12.91400 12.89832 12.87905 12.85683
## [441] 12.83232 12.80616 12.77899 12.75148 12.72425 12.69797 12.67329 12.65084
## [449] 12.63128 12.61525 12.59969 12.58139 12.56087 12.53860 12.51509 12.49082
## [457] 12.46630 12.44201 12.41844 12.39610 12.37547 12.35705 12.34133 12.32880
## [465] 12.31790 12.30682 12.29566 12.28455 12.27359 12.26291 12.25262 12.24283
## [473] 12.23366 12.22522 12.21763 12.21101 12.20546 12.20111 12.19752 12.19422
## [481] 12.19124 12.18863 12.18643 12.18469 12.18346 12.18276 12.18266 12.18318
## [489] 12.18438 12.18630 12.18899 12.19247 12.19668 12.20148 12.20687 12.21286
## [497] 12.21945 12.22662 12.23439 12.24274 12.25169 12.26123 12.27135 12.28207
## [505] 12.29337 12.30525 12.31772 12.33077 12.34441 12.35863 12.37343 12.38882
## [513] 12.40478 12.42132 12.43844 12.45613 12.47441 12.49326 12.51268 12.53268
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 520)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.57571 12.57366 12.57170 12.56982 12.56801 12.56626 12.56458 12.56296
## [9] 12.56138 12.55986 12.55838 12.55693 12.55552 12.55413 12.55277 12.55142
## [17] 12.55008 12.54875 12.54742 12.54609 12.54475 12.54340 12.54204 12.54069
## [25] 12.53935 12.53802 12.53671 12.53542 12.53415 12.53290 12.53168 12.53049
## [33] 12.52933 12.52820 12.52711 12.52605 12.52504 12.52407 12.52315 12.52228
## [41] 12.52146 12.52069 12.51998 12.51933 12.51874 12.51822 12.51776 12.51737
## [49] 12.51706 12.51681 12.51656 12.51622 12.51580 12.51532 12.51478 12.51420
## [57] 12.51360 12.51298 12.51235 12.51173 12.51113 12.51056 12.51004 12.50957
## [65] 12.50918 12.50886 12.50863 12.50851 12.50851 12.50864 12.50890 12.50933
## [73] 12.50991 12.51068 12.51163 12.51278 12.51415 12.51575 12.51746 12.51916
## [81] 12.52087 12.52259 12.52431 12.52606 12.52782 12.52960 12.53142 12.53327
## [89] 12.53515 12.53708 12.53905 12.54108 12.54316 12.54531 12.54751 12.54979
## [97] 12.55214 12.55457 12.55709 12.55969 12.56238 12.56517 12.56806 12.57106
## [105] 12.57416 12.57738 12.57993 12.58113 12.58112 12.58004 12.57804 12.57528
## [113] 12.57189 12.56802 12.56382 12.55943 12.55501 12.55069 12.54662 12.54296
## [121] 12.53983 12.53741 12.53581 12.53521 12.53573 12.53753 12.54076 12.54555
## [129] 12.55256 12.56208 12.57375 12.58721 12.60207 12.61798 12.63457 12.65147
## [137] 12.66832 12.68474 12.70038 12.71486 12.72782 12.73888 12.75098 12.76689
## [145] 12.78601 12.80774 12.83149 12.85665 12.88262 12.90882 12.93463 12.95946
## [153] 12.98272 13.00380 13.02211 13.03705 13.05202 13.07040 13.09160 13.11501
## [161] 13.14002 13.16603 13.19242 13.21860 13.24395 13.26786 13.28974 13.30898
## [169] 13.32496 13.33708 13.34730 13.35790 13.36879 13.37988 13.39106 13.40225
## [177] 13.41335 13.42426 13.43489 13.44514 13.45492 13.46412 13.47267 13.48045
## [185] 13.48738 13.49336 13.49830 13.50209 13.50465 13.50588 13.50568 13.50396
## [193] 13.50062 13.49556 13.48870 13.47994 13.46863 13.45443 13.43772 13.41884
## [201] 13.39817 13.37605 13.35286 13.32895 13.30468 13.28042 13.25652 13.23334
## [209] 13.21125 13.19061 13.16837 13.14169 13.11126 13.07775 13.04185 13.00424
## [217] 12.96559 12.92658 12.88791 12.85023 12.81424 12.78062 12.75005 12.72320
## [225] 12.69559 12.66289 12.62599 12.58577 12.54314 12.49898 12.45418 12.40963
## [233] 12.36624 12.32488 12.28644 12.25183 12.22193 12.19764 12.17636 12.15500
## [241] 12.13371 12.11262 12.09187 12.07160 12.05194 12.03304 12.01503 11.99804
## [249] 11.98222 11.96771 11.95464 11.94314 11.93409 11.92800 11.92450 11.92319
## [257] 11.92372 11.92569 11.92873 11.93246 11.93651 11.94049 11.94403 11.94675
## [265] 11.94827 11.94821 11.94987 11.95627 11.96667 11.98028 11.99637 12.01415
## [273] 12.03288 12.05179 12.07012 12.08711 12.10200 12.11403 12.12243 12.12645
## [281] 12.13000 12.13709 12.14705 12.15924 12.17299 12.18765 12.20257 12.21710
## [289] 12.23057 12.24233 12.25173 12.25812 12.26083 12.25922 12.25385 12.24597
## [297] 12.23589 12.22397 12.21053 12.19591 12.18045 12.16448 12.14833 12.13235
## [305] 12.11686 12.10221 12.08873 12.07675 12.06311 12.04490 12.02285 11.99769
## [313] 11.97012 11.94089 11.91071 11.88031 11.85040 11.82172 11.79498 11.77092
## [321] 11.75025 11.73369 11.71761 11.69827 11.67623 11.65206 11.62632 11.59959
## [329] 11.57242 11.54538 11.51904 11.49397 11.47073 11.44988 11.43200 11.41766
## [337] 11.40741 11.39854 11.38824 11.37692 11.36500 11.35290 11.34102 11.32980
## [345] 11.31964 11.31095 11.30416 11.29968 11.29793 11.29932 11.30339 11.30931
## [353] 11.31690 11.32603 11.33652 11.34824 11.36101 11.37468 11.38910 11.40412
## [361] 11.41957 11.43529 11.45114 11.46696 11.48513 11.50776 11.53429 11.56415
## [369] 11.59678 11.63162 11.66810 11.70566 11.74374 11.78178 11.81920 11.85545
## [377] 11.88996 11.92217 11.95152 11.97744 12.00356 12.03343 12.06643 12.10191
## [385] 12.13921 12.17769 12.21672 12.25564 12.29381 12.33058 12.36532 12.39736
## [393] 12.42608 12.45082 12.47448 12.50011 12.52731 12.55567 12.58481 12.61430
## [401] 12.64375 12.67275 12.70091 12.72782 12.75307 12.77627 12.79700 12.81488
## [409] 12.83152 12.84868 12.86614 12.88371 12.90116 12.91829 12.93489 12.95075
## [417] 12.96565 12.97940 12.99177 13.00256 13.01156 13.01856 13.02323 13.02554
## [425] 13.02575 13.02412 13.02090 13.01635 13.01071 13.00425 12.99722 12.98986
## [433] 12.98244 12.97521 12.96843 12.96234 12.95557 12.94673 12.93606 12.92381
## [441] 12.91023 12.89556 12.88005 12.86394 12.84749 12.83093 12.81452 12.79850
## [449] 12.78311 12.76861 12.75234 12.73194 12.70813 12.68158 12.65301 12.62311
## [457] 12.59257 12.56210 12.53239 12.50414 12.47804 12.45481 12.43512 12.41968
## [465] 12.40597 12.39113 12.37543 12.35911 12.34242 12.32562 12.30895 12.29266
## [473] 12.27701 12.26224 12.24861 12.23635 12.22573 12.21700 12.20926 12.20150
## [481] 12.19381 12.18626 12.17892 12.17187 12.16518 12.15894 12.15321 12.14807
## [489] 12.14359 12.13986 12.13695 12.13493 12.13368 12.13301 12.13292 12.13341
## [497] 12.13449 12.13615 12.13838 12.14118 12.14456 12.14852 12.15304 12.15813
## [505] 12.16379 12.17001 12.17679 12.18414 12.19205 12.20051 12.20954 12.21911
## [513] 12.22924 12.23992 12.25115 12.26293 12.27526 12.28813 12.30154 12.31550
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 520)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.83746 11.84192 11.84644 11.85098 11.85555 11.86011 11.86466 11.86917
## [9] 11.87365 11.87806 11.88239 11.88664 11.89078 11.89479 11.89867 11.90240
## [17] 11.90595 11.90933 11.91250 11.91546 11.91819 11.92068 11.92290 11.92485
## [25] 11.92651 11.92786 11.92889 11.92958 11.92992 11.92995 11.92974 11.92929
## [33] 11.92862 11.92774 11.92667 11.92542 11.92401 11.92245 11.92075 11.91893
## [41] 11.91701 11.91498 11.91288 11.91071 11.90849 11.90622 11.90394 11.90164
## [49] 11.89934 11.89707 11.89482 11.89261 11.89047 11.88840 11.88641 11.88452
## [57] 11.88275 11.88067 11.87788 11.87444 11.87040 11.86581 11.86073 11.85521
## [65] 11.84931 11.84308 11.83658 11.82985 11.82296 11.81595 11.80888 11.80181
## [73] 11.79478 11.78786 11.78109 11.77454 11.76825 11.76227 11.75667 11.75150
## [81] 11.74680 11.74264 11.73907 11.73614 11.73391 11.73121 11.72695 11.72126
## [89] 11.71427 11.70614 11.69698 11.68693 11.67614 11.66474 11.65286 11.64064
## [97] 11.62822 11.61573 11.60332 11.59110 11.57923 11.56784 11.55706 11.54704
## [105] 11.53790 11.52978 11.52282 11.51716 11.51293 11.51027 11.50931 11.51019
## [113] 11.51305 11.51783 11.52428 11.53229 11.54171 11.55243 11.56432 11.57725
## [121] 11.59109 11.60571 11.62099 11.63679 11.65299 11.66947 11.68609 11.70272
## [129] 11.71924 11.73552 11.75143 11.76684 11.78163 11.79853 11.81994 11.84516
## [137] 11.87354 11.90438 11.93703 11.97080 12.00502 12.03902 12.07213 12.10366
## [145] 12.13294 12.15931 12.18209 12.20483 12.23115 12.26044 12.29209 12.32551
## [153] 12.36008 12.39521 12.43028 12.46469 12.49784 12.52912 12.55793 12.58366
## [161] 12.60571 12.62647 12.64854 12.67161 12.69539 12.71955 12.74380 12.76782
## [169] 12.79132 12.81399 12.83551 12.85559 12.87392 12.89018 12.90409 12.91693
## [177] 12.93016 12.94366 12.95731 12.97100 12.98461 12.99803 13.01113 13.02381
## [185] 13.03595 13.04743 13.05814 13.06795 13.07677 13.08446 13.09091 13.09602
## [193] 13.09965 13.10171 13.10206 13.10060 13.09720 13.09177 13.08440 13.07540
## [201] 13.06485 13.05287 13.03957 13.02506 13.00944 12.99283 12.97533 12.95705
## [209] 12.93810 12.91859 12.89863 12.87832 12.85455 12.82474 12.78984 12.75076
## [217] 12.70844 12.66382 12.61781 12.57136 12.52539 12.48084 12.43864 12.39971
## [225] 12.36499 12.33541 12.30564 12.27034 12.23050 12.18709 12.14108 12.09344
## [233] 12.04515 11.99716 11.95046 11.90602 11.86480 11.82779 11.79594 11.77024
## [241] 11.74686 11.72160 11.69489 11.66717 11.63885 11.61036 11.58214 11.55460
## [249] 11.52817 11.50328 11.48036 11.45983 11.44211 11.42764 11.41704 11.41028
## [257] 11.40685 11.40621 11.40783 11.41118 11.41572 11.42093 11.42627 11.43121
## [265] 11.43523 11.43779 11.43835 11.43640 11.43481 11.43661 11.44152 11.44925
## [273] 11.45949 11.47196 11.48636 11.50240 11.51977 11.53820 11.55738 11.57702
## [281] 11.59682 11.61650 11.63576 11.65429 11.67182 11.68805 11.70267 11.71540
## [289] 11.72595 11.73401 11.73930 11.74152 11.74038 11.73558 11.72570 11.71008
## [297] 11.68960 11.66514 11.63757 11.60777 11.57661 11.54496 11.51371 11.48372
## [305] 11.45587 11.43104 11.41010 11.39393 11.37713 11.35442 11.32681 11.29530
## [313] 11.26092 11.22465 11.18753 11.15056 11.11475 11.08111 11.05065 11.02439
## [321] 11.00333 10.98848 10.97708 10.96576 10.95461 10.94376 10.93331 10.92336
## [329] 10.91402 10.90539 10.89760 10.89073 10.88491 10.88023 10.87680 10.87474
## [337] 10.87414 10.87522 10.87800 10.88231 10.88800 10.89491 10.90286 10.91170
## [345] 10.92126 10.93137 10.94189 10.95264 10.96346 10.97418 10.98635 11.00135
## [353] 11.01883 11.03840 11.05971 11.08237 11.10603 11.13030 11.15483 11.17923
## [361] 11.20315 11.22620 11.24803 11.26826 11.28880 11.31163 11.33645 11.36296
## [369] 11.39087 11.41989 11.44972 11.48008 11.51067 11.54119 11.57135 11.60086
## [377] 11.62942 11.65675 11.68254 11.70651 11.73017 11.75507 11.78098 11.80766
## [385] 11.83488 11.86241 11.89001 11.91745 11.94449 11.97090 11.99645 12.02090
## [393] 12.04401 12.06556 12.08774 12.11259 12.13959 12.16824 12.19804 12.22849
## [401] 12.25907 12.28929 12.31864 12.34661 12.37271 12.39642 12.41724 12.43467
## [409] 12.45042 12.46641 12.48248 12.49847 12.51420 12.52951 12.54422 12.55817
## [417] 12.57120 12.58313 12.59380 12.60303 12.61067 12.61654 12.62027 12.62180
## [425] 12.62136 12.61922 12.61562 12.61083 12.60510 12.59867 12.59181 12.58477
## [433] 12.57781 12.57117 12.56512 12.55990 12.55354 12.54421 12.53235 12.51837
## [441] 12.50274 12.48587 12.46822 12.45021 12.43229 12.41489 12.39846 12.38342
## [449] 12.37021 12.35928 12.34847 12.33554 12.32087 12.30480 12.28769 12.26989
## [457] 12.25176 12.23365 12.21591 12.19890 12.18298 12.16849 12.15580 12.14525
## [465] 12.13537 12.12458 12.11311 12.10115 12.08892 12.07662 12.06447 12.05268
## [473] 12.04146 12.03101 12.02154 12.01327 12.00641 12.00116 11.99679 11.99246
## [481] 11.98824 11.98420 11.98039 11.97689 11.97375 11.97105 11.96885 11.96721
## [489] 11.96619 11.96588 11.96632 11.96758 11.96953 11.97200 11.97499 11.97849
## [497] 11.98250 11.98702 11.99204 11.99757 12.00361 12.01014 12.01718 12.02471
## [505] 12.03275 12.04127 12.05029 12.05980 12.06980 12.08029 12.09127 12.10272
## [513] 12.11466 12.12709 12.13999 12.15336 12.16722 12.18154 12.19634 12.21161
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")